﻿
using System.Collections.Generic;
namespace SanguoSharp.Server
{
    /// <summary>
    /// 所有人都响应的牌 南蛮入侵 万箭齐发 五谷丰登 桃园结义
    /// </summary>
    public abstract class AllPlayersCard : PlayableCard
    {
        private bool includeSelf;

        protected AllPlayersCard(Game game, int id, CardType type, CardSuit suit, CardRank rank, bool includeSelf)
            : base(game, id, type, suit, rank)
        {
            this.includeSelf = includeSelf;
        }
        protected AllPlayersCard(Game game, int id, CardType type, CardSuit suit, CardRank rank)
            : this(game, id, type, suit, rank, false)
        {
        }

        protected override void OnPlay()
        {
            Player owner = Owner;
            Player current = owner;
            Game.GameTable.PlayerPlayCard(this);

            if (!includeSelf)
                current = Game.NextPlayer(current);

            List<ResponseHandler> handlers = new List<ResponseHandler>();
            do
            {
                if (current.HasCardEffect(this))
                {
                    ResponseHandler h = OnPlay(owner, current);
                    if (h != null)
                        handlers.Add(h);
                }
                current = Game.NextPlayer(current);
            }
            while (current != owner);
            if (handlers.Count != 0)
                Game.GameCycle.PushTempHandler(new QueueResponseHandler(handlers));
        }
        protected override void OnPlayVirtually(Card card)
        {
            Player owner = card.Owner;
            Player current = owner;
            Game.GameTable.PlayerPlayCard(card, this.Type);

            if (!includeSelf)
                current = Game.NextPlayer(current);

            List<ResponseHandler> handlers = new List<ResponseHandler>();
            do
            {
                if (current.HasCardEffect(this))
                {
                    ResponseHandler h = OnPlay(owner, current);
                    if (h != null)
                        handlers.Add(h);
                }
                current = Game.NextPlayer(current);
            }
            while (current != owner);
            if (handlers.Count != 0)
                Game.GameCycle.PushTempHandler(new QueueResponseHandler(handlers));
        }

        protected abstract ResponseHandler OnPlay(Player owner, Player targetPlayer);
    }
}